<?php
class ExportController extends Controller {
	/**
	 * /?r=export/index&name=model&pageSize=1000&currentPage=0
	 */
	public function actionIndex() {
		$pageSize = Yii::app ()->request->getParam ( 'pageSize' );
		$currentPage = Yii::app ()->request->getParam ( 'currentPage' );
		$name = Yii::app ()->request->getParam ( 'name' );
		if (empty ( $pageSize ))
			$pageSize = 10;
		if (empty ( $currentPage ))
			$currentPage = 0;
		$criteria = new CDbCriteria ();
		if (class_exists ( $name )) {
			$name = new $name ();
			$primaryKey = $name->tableSchema->primaryKey;
			if (! empty ( $primaryKey )) {
				$criteria->order = $primaryKey . ' desc';
			} else {
				$columns = $name->attributeNames ();
				$criteria->order = $columns [0] . ' desc';
			}
			$data = new CActiveDataProvider ( get_class ( $name ), array (
					'criteria' => $criteria,
					'pagination' => array (
							'pageSize' => $pageSize,
							'currentPage' => $currentPage 
					) 
			) );
			echo "<!--\n";
			foreach ( $data->getData () as $value ) {
				// print_r($value->attributes);
				if (! empty ( $value->attributes ) && is_array ( $value->attributes )) {
					$value->attributes = array_map ( 'addslashes', $value->attributes );
					$sql = Base::multiInsert ( $name->tableName (), $value->attributes );
					$sql = preg_replace ( '/insert\s*?into/i', 'REPLACE INTO', $sql );
					echo $sql . ";";
				}
			}
			echo "\n<BR>//-->";
		}
	}
}
?>